#include "ChessPanelRecView.h"

ChessPanelRecView::ChessPanelRecView(QWidget *parent) : QWidget(parent){
    init();
}

void ChessPanelRecView::init() {
    setGeometry(664, 20, 1080-664, 665);

    QGridLayout * layout = new QGridLayout;
    layout->setAlignment(Qt::AlignTop);

    layout->addWidget(groupGameList(), 0, 0);
    layout->addWidget(groupGameInfo(), 1, 0);

    setLayout(layout);
}

QGroupBox * ChessPanelRecView::groupGameList() {
    QGroupBox * groupGameList = new QGroupBox(tr("Lista gier:"), this);
    QVBoxLayout * mainLayout  = new QVBoxLayout;
    QGridLayout * layout      = new QGridLayout;

    groupGameList->setStyleSheet("font-size: 15px");

    comboGameList = new QComboBox(this);
    comboGameList->setStyleSheet("height: 30px; font-size: 15px");
    //comboGameList->addItem("Wybierz rozgrywkę");

    connect(comboGameList, SIGNAL(currentIndexChanged(int)), this, SLOT(gameListIndexChanged(int)));

    buttonStart = new QPushButton(tr("Odtwórz"), this);
    buttonStart->setFixedHeight(30);
    buttonStart->setEnabled(false);
    buttonPause = new QPushButton(tr("Zatrzymaj"), this);
    buttonPause->setFixedHeight(30);
    buttonPause->setEnabled(false);
    buttonStop  = new QPushButton(tr("Przerwij"), this);
    buttonStop->setFixedHeight(30);
    buttonStop->setEnabled(false);

    connect(buttonStart, SIGNAL(clicked()), this, SLOT(start()), Qt::QueuedConnection);
    connect(buttonPause, SIGNAL(clicked()), this, SLOT(pauseOrResume()), Qt::QueuedConnection);
    connect(buttonStop, SIGNAL(clicked()), this, SLOT(stopSimulation()), Qt::QueuedConnection);

    groupGameList->setLayout(mainLayout);

    layout->addWidget(comboGameList, 0, 0);

    QGridLayout * layout2 = new QGridLayout;

    mainLayout->setSpacing(15);
    layout2->addWidget(buttonStart, 0, 1);
    layout2->addWidget(buttonPause, 0, 2);
    layout2->addWidget(buttonStop, 0, 3);

    mainLayout->addLayout(layout);
    mainLayout->addLayout(layout2);

    return groupGameList;
}

QGroupBox * ChessPanelRecView::groupGameInfo() {
    QGroupBox * groupGameInfo = new QGroupBox(tr("Szczegóły rozgrywki:"), this);
    QGridLayout * layout      = new QGridLayout;

    groupGameInfo->setStyleSheet("font-size: 15px");

    QLabel * labelEventDesc = new QLabel(tr("Turniej: "), this);
    QLabel * labelSiteDesc = new QLabel(tr("Miejscowość: "), this);
    QLabel * labelDateDesc = new QLabel(tr("Data: "), this);
    QLabel * labelRoundDesc = new QLabel(tr("Runda: "), this);
    QLabel * labelPlayerWhiteDesc = new QLabel(tr("Białe: "), this);
    QLabel * labelPlayerBlackDesc = new QLabel(tr("Czarne: "), this);
    QLabel * labelResultDesc = new QLabel(tr("Wynik partii: "), this);
    QLabel * labelECODesc = new QLabel(tr("Kod debiutowy ECO: "), this);
    QLabel * labelWhiteEloDesc = new QLabel(tr("Ranking ELO białe: "), this);
    QLabel * labelBlackEloDesc = new QLabel(tr("Ranking ELO czarne: "), this);
    //QLabel * labelAnnotatorDesc = new QLabel(tr("Komentator: "), this);

    labelEvent = new QLabel(tr(""), this);
    labelSite = new QLabel(tr(""), this);
    labelDate = new QLabel(tr(""), this);
    labelRound = new QLabel(tr(""), this);
    labelPlayerWhite = new QLabel(tr(""), this);
    labelPlayerBlack = new QLabel(tr(""), this);
    labelResult = new QLabel(tr(""), this);
    labelECO = new QLabel(tr(""), this);
    labelWhiteElo = new QLabel(tr(""), this);
    labelBlackElo = new QLabel(tr(""), this);
    //labelAnnotator = new QLabel(tr(""), this);

    groupGameInfo->setLayout(layout);

    layout->addWidget(labelEventDesc, 0, 0);
    layout->addWidget(labelSiteDesc, 1, 0);
    layout->addWidget(labelDateDesc, 2, 0);
    layout->addWidget(labelRoundDesc, 3, 0);
    layout->addWidget(labelPlayerWhiteDesc, 4, 0);
    layout->addWidget(labelPlayerBlackDesc, 5, 0);
    layout->addWidget(labelResultDesc, 6, 0);
    layout->addWidget(labelECODesc, 7, 0);
    layout->addWidget(labelWhiteEloDesc, 8, 0);
    layout->addWidget(labelBlackEloDesc, 9, 0);
    //layout->addWidget(labelAnnotatorDesc, 10, 0);

    layout->addWidget(labelEvent, 0, 1);
    layout->addWidget(labelSite, 1, 1);
    layout->addWidget(labelDate, 2, 1);
    layout->addWidget(labelRound, 3, 1);
    layout->addWidget(labelPlayerWhite, 4, 1);
    layout->addWidget(labelPlayerBlack, 5, 1);
    layout->addWidget(labelResult, 6, 1);
    layout->addWidget(labelECO, 7, 1);
    layout->addWidget(labelWhiteElo, 8, 1);
    layout->addWidget(labelBlackElo, 9, 1);
    //layout->addWidget(labelAnnotator, 10, 1);

    return groupGameInfo;
}


void ChessPanelRecView::addGameToList(const QString &game)
{
    comboGameList->addItem(game);

    if(!buttonStart->isEnabled())
        buttonStart->setEnabled(true);
}

void ChessPanelRecView::setGameInfo(const QMap<QString, QString> &gameInfo) {
    labelEvent->setText(gameInfo["Event"]);
    labelSite->setText(gameInfo["Site"]);
    labelDate->setText(gameInfo["Date"]);
    labelRound->setText(gameInfo["Round"]);
    labelPlayerWhite->setText(gameInfo["White"]);
    labelPlayerBlack->setText(gameInfo["Black"]);
    labelResult->setText(gameInfo["Result"]);
    labelECO->setText(gameInfo["ECO"]);
    labelWhiteElo->setText(gameInfo["WhiteElo"]);
    labelBlackElo->setText(gameInfo["BlackElo"]);
    //labelAnnotator->setText(gameInfo["Round"]);
}

void ChessPanelRecView::gameListIndexChanged(int index) {
    emit gameListIndexChangedSignal(index);
}


void ChessPanelRecView::start()
{
    buttonStart->setEnabled(false);
    comboGameList->setEnabled(false);

    buttonStop->setEnabled(true);
    buttonPause->setEnabled(true);

    emit startSimulation();
}

void ChessPanelRecView::pauseOrResume()
{
    if(buttonPause->text() == "Zatrzymaj") {
        buttonPause->setText("Wznów");
        emit pause();
    } else {
        buttonPause->setText("Zatrzymaj");
        emit resume();
    }
}

void ChessPanelRecView::stopSimulation()
{
    buttonPause->setEnabled(false);
    buttonPause->setText("Zatrzymaj");
    comboGameList->setEnabled(true);

    buttonStop->setEnabled(false);
    buttonStart->setEnabled(true);

    emit stop();
}
